<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-hemisu.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-hemisu.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-hemisu.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="js,前端,读书笔记,">










<meta name="description" content="# 类型 ## 内置类型  null undefined boolean number string object symbol  1typeof null === &quot;object&quot;: // true 历史遗留问题，用复合条件来检测null值的类型： 12var a = null;(!a &amp;amp;&amp;amp; typeof a === &quot;object&quot;); // true 变量是没有类型的">
<meta name="keywords" content="js,前端,读书笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="你不知道的JS中卷 笔记">
<meta property="og:url" content="http://www.hemisu.com/2018/04/14/2018-04-14 YDNJS-mid/index.html">
<meta property="og:site_name" content="何米酥`s Blog">
<meta property="og:description" content="# 类型 ## 内置类型  null undefined boolean number string object symbol  1typeof null === &quot;object&quot;: // true 历史遗留问题，用复合条件来检测null值的类型： 12var a = null;(!a &amp;amp;&amp;amp; typeof a === &quot;object&quot;); // true 变量是没有类型的">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-09-27T05:22:33.708Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="你不知道的JS中卷 笔记">
<meta name="twitter:description" content="# 类型 ## 内置类型  null undefined boolean number string object symbol  1typeof null === &quot;object&quot;: // true 历史遗留问题，用复合条件来检测null值的类型： 12var a = null;(!a &amp;amp;&amp;amp; typeof a === &quot;object&quot;); // true 变量是没有类型的">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://www.hemisu.com/2018/04/14/2018-04-14 YDNJS-mid/">





  <title>你不知道的JS中卷 笔记 | 何米酥`s Blog</title>
  








  <!-- Hotjar Tracking Code for www.hemisu.com -->
  <script>
      (function(h,o,t,j,a,r){
          h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
          h._hjSettings={hjid:1933736,hjsv:6};
          a=o.getElementsByTagName('head')[0];
          r=o.createElement('script');r.async=1;
          r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
          a.appendChild(r);
      })(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');
  </script>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">何米酥`s Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">EFE</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.hemisu.com/2018/04/14/2018-04-14 YDNJS-mid/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="何米酥">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="何米酥`s Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">你不知道的JS中卷 笔记</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-04-14T17:06:27+00:00">
                2018-04-14
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/前端/" itemprop="url" rel="index">
                    <span itemprop="name">前端</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/前端/笔记/" itemprop="url" rel="index">
                    <span itemprop="name">笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p># 类型</p>
<p>## 内置类型</p>
<ul>
<li>null</li>
<li>undefined</li>
<li>boolean</li>
<li>number</li>
<li>string</li>
<li>object</li>
<li>symbol</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typeof</span> <span class="literal">null</span> === <span class="string">"object"</span>: <span class="comment">// true</span></span><br></pre></td></tr></table></figure>
<p>历史遗留问题，用复合条件来检测null值的类型：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="literal">null</span>;</span><br><span class="line">(!a &amp;&amp; <span class="keyword">typeof</span> a === <span class="string">"object"</span>); <span class="comment">// true</span></span><br></pre></td></tr></table></figure>
<p>变量是没有类型的，只有值才有，对变量执行typeof操作时，得到的结果是该变量持有的值的类型</p>
<p>已在作用域中声明但还没有赋值的变量，是undefined的。相反，还没有在作用域中声明过的变量是undeclared的。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typeof</span> a; <span class="comment">// undefined</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typeof</span> b; <span class="comment">// undefined (其实应当返回undeclared, typeof的安全防范机制)</span></span><br></pre></td></tr></table></figure>
<h2 id="typeof-undeclared"><a href="#typeof-undeclared" class="headerlink" title="typeof undeclared"></a>typeof undeclared</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 这样会抛出错误</span></span><br><span class="line"><span class="keyword">if</span>(DEBUG) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">"Debugging is staring"</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 这样是安全的</span></span><br><span class="line"><span class="keyword">if</span>(<span class="keyword">typeof</span> DEBUG !== <span class="string">"undefined"</span>) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">"Debugging is starting"</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&lt;!--more--&gt;</span><br></pre></td></tr></table></figure>
<p>在没有全局变量可用的时候，使用typeof的安全防范机制做检查；或是使用“依赖注入”(dependency injection)设计模式将参数显式地传递到函数中</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">doSomethingCool</span>(<span class="params">Feature</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> helper = Feature || <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; <span class="comment">/* .. default feature .. */</span> &#125;;</span><br><span class="line">  <span class="keyword">var</span> val = helper();</span><br><span class="line">  <span class="comment">// ..</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h2><ul>
<li>变量没有类型，但它们持有的值有类型。类型定义了值的行为特征。</li>
<li>undeclared表示变量还没有被声明过</li>
<li>访问undeclared变量时会报错”ReferenceError: a is not defined”,并且typeof因为安全防范机制，对于undeclared、undefined都返回undefined</li>
</ul>
<h1 id="值"><a href="#值" class="headerlink" title="值"></a>值</h1><h2 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h2><p>使用delete运算符可以将单元从数组中删除，但是单元删除后，数组的length属性不会发生变化。</p>
<p>创建“稀疏”数组时，要注意其中的空白单元”empty slot”</p>
<p>数组通过数字进行索引，又去的是他们也是对象，所以也可以包含字符串键值和属性，但是不计算在数组长度内。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [];</span><br><span class="line"></span><br><span class="line">a[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line">a[<span class="string">"foobar"</span>] = <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">a.length; <span class="comment">// 1</span></span><br><span class="line">a[<span class="string">"length"</span>]; <span class="comment">// 2</span></span><br><span class="line">a.foobar; <span class="comment">// 2</span></span><br></pre></td></tr></table></figure>
<p><strong>特别注意</strong>如果字符串键值能够被强制类型转换为十进制的数字，它会被当作数字索引来处理<br>所以建议使用对象来存放键值/属性值，用数组来存放数字索引值。</p>
<h1 id="类数组"><a href="#类数组" class="headerlink" title="类数组"></a>类数组</h1><p>有时候需要将类数组（一组通过数组索引的值）转换为真正的数组，这一般是通过数组工具函数实现</p>
<p>工具函数slice(..)经常被用于这一转换<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> arr = <span class="built_in">Array</span>.prototype.slice.call( <span class="built_in">arguments</span> ); <span class="comment">// slice返回的是一个数组的副本</span></span><br><span class="line">  arr.push( <span class="string">"bam"</span> );</span><br><span class="line">  <span class="built_in">console</span>.log( arr );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>用ES6中的内置工具函数Array.from(..)可以实现同样的功能</p>
<h2 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h2><p>字符串和数组很相似，都是类数组，都有length属性以及indexOf(..)和concat(..)方法<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">"foo"</span>;</span><br><span class="line"><span class="keyword">var</span> b = [<span class="string">"f"</span>, <span class="string">"o"</span>, <span class="string">"o"</span>];</span><br></pre></td></tr></table></figure></p>
<p>字符串不是字符数组，字符串是不可改变的，而数组是可以改变的。<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">a[<span class="number">1</span>] = <span class="string">"0"</span>; <span class="comment">// 应当使用a.charAt(1) 获取</span></span><br><span class="line">b[<span class="number">1</span>] = <span class="string">"0"</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">a; <span class="comment">// "foo</span></span><br><span class="line">b; <span class="comment">// ["f", "o", "o"]</span></span><br></pre></td></tr></table></figure></p>
<p>字符串不可变是指字符串的成员函数不会改变其原始值，而是创建并返回一个新的字符串。而数组的成员函数都是在其原始值上进行操作。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">c = a.toUpperCase();</span><br><span class="line">a === c; <span class="comment">//false</span></span><br><span class="line">a; <span class="comment">// "foo"</span></span><br><span class="line">c; <span class="comment">// "FOO"</span></span><br><span class="line"></span><br><span class="line">b.push( <span class="string">"!"</span> );</span><br><span class="line">b; <span class="comment">// ["f", "o", "o", "!"]</span></span><br></pre></td></tr></table></figure>
<p>许多数组函数用来处理字符串很方便，虽然字符串没有这些函数，但可以通过“借用”数组的非变更方法来处理字符串<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">a.join; <span class="comment">// undefined</span></span><br><span class="line">a.map; <span class="comment">// undefined</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> c = <span class="built_in">Array</span>.prototype.join.call(a, <span class="string">"-"</span>);</span><br><span class="line"><span class="keyword">var</span> d = <span class="built_in">Array</span>.prototype.map.call(a, <span class="function"><span class="keyword">function</span>(<span class="params">v</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> v.toUpperCase() + <span class="string">"."</span>;</span><br><span class="line">&#125;).join( <span class="string">""</span> );</span><br><span class="line"></span><br><span class="line">c; <span class="comment">// "f-o-o"</span></span><br><span class="line">d; <span class="comment">// "F.O.O."</span></span><br></pre></td></tr></table></figure></p>
<p>字符串反转<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Array</span>.prototype.reverse.call(a); <span class="comment">// 返回值仍然是"foo"的一个封装对象</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// other way</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> c = a.split(<span class="string">""</span>).reverse().join(<span class="string">""</span>);</span><br><span class="line">c; <span class="comment">// oof</span></span><br></pre></td></tr></table></figure></p>
<h2 id="数字"><a href="#数字" class="headerlink" title="数字"></a>数字</h2><p><code>.</code>运算符是一个有效的数字字符，回被优先识别为数字常量的一部分，然后才是对象属性访问运算符。<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">42.</span>toFixed(<span class="number">3</span>); <span class="comment">// SyntaxError</span></span><br><span class="line"></span><br><span class="line">(<span class="number">42</span>).toFixed(<span class="number">3</span>); <span class="comment">// "42.000"</span></span><br><span class="line"><span class="number">0.42</span>.toFixed(<span class="number">3</span>); <span class="comment">// "0.420"</span></span><br><span class="line"><span class="number">42.</span>.toFixed(<span class="number">3</span>); <span class="comment">// "42.000"</span></span><br></pre></td></tr></table></figure></p>
<h3 id="0-1-0-2-0-3问题"><a href="#0-1-0-2-0-3问题" class="headerlink" title="0.1+0.2 != 0.3问题"></a>0.1+0.2 != 0.3问题</h3><p>最常见的方法是设置一个误差范围值，通常称为机器精度(machine epsilon)</p>
<p>对于JS的数字来说，这个值通常是2^-52</p>
<p>ES6中内置了这个数字Number.EPSILON，或者写成Math.pow(2, -52);<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//polyfill</span></span><br><span class="line"><span class="keyword">if</span> (!<span class="built_in">Number</span>.EPSILON) &#123;</span><br><span class="line">  <span class="built_in">Number</span>.EPSILON = <span class="built_in">Math</span>.pow(<span class="number">2</span>, <span class="number">-52</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>于是我们可以解决0.1+0.2 != 0.3这个问题了<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">numbersCloseEnoughToEqual</span>(<span class="params">n1, n2</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">Math</span>.abs(n1 - n2) &lt; <span class="built_in">Number</span>.EPSILON;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> a = <span class="number">0.1</span> + <span class="number">0.2</span>;</span><br><span class="line"><span class="keyword">var</span> b = <span class="number">0.3</span>;</span><br><span class="line">numbersCloseEnoughToEqual(a, b); <span class="comment">// true</span></span><br></pre></td></tr></table></figure></p>
<h3 id="整数的安全范围"><a href="#整数的安全范围" class="headerlink" title="整数的安全范围"></a>整数的安全范围</h3><p>数字的呈现方式导致整数的安全范围远小于Number.MAX_VALUE （1.7976931348623157e+308）<br>能够被安全呈现的最大整数是2^53 - 1(去除1符号位，11位指数，还有52位) Number.MAX_SAFE_INTEGER (9007199254740991)</p>
<h3 id="整数检测"><a href="#整数检测" class="headerlink" title="整数检测"></a>整数检测</h3><p>ES6 中 Number.isInteger(..)<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Number</span>.isInteger(<span class="number">42</span>); <span class="comment">// true</span></span><br><span class="line"><span class="built_in">Number</span>.isInteger(<span class="number">42.000</span>); <span class="comment">// true</span></span><br><span class="line"><span class="built_in">Number</span>.isInteger(<span class="number">42.3</span>); <span class="comment">// false</span></span><br></pre></td></tr></table></figure></p>
<p>polyfill方法<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (!<span class="built_in">Number</span>.isInteger) &#123;</span><br><span class="line">  <span class="built_in">Number</span>.isInteger = <span class="function"><span class="keyword">function</span>(<span class="params">num</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">typeof</span> num == <span class="string">"number"</span> &amp;&amp; num % <span class="number">1</span> == <span class="number">0</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="32位有符号整数"><a href="#32位有符号整数" class="headerlink" title="32位有符号整数"></a>32位有符号整数</h3><p>虽然整数最大能够达到53位，但是有些数字操作（如数位操作）只适用于32位数字，所以在这些操作中数字的安全范围就回从</p>
<p>Math.pow(-2,31)~Math.pow(2,31)</p>
<p>-2147483648 ~ 2147483648</p>
<p>如<code>a | 0</code>可以将变量a中的数值转换为32位有符号整数，因为数位操作符<code>|</code>只适用于32位整数。因此与0进行操作可以截取a中的32位数位。</p>
<blockquote>
<p>某些特殊的值并不是32位安全范围的，比如NaN和Infinity</p>
</blockquote>
<h2 id="特殊数值"><a href="#特殊数值" class="headerlink" title="特殊数值"></a>特殊数值</h2><h3 id="undefined"><a href="#undefined" class="headerlink" title="undefined"></a>undefined</h3><p>在非严格模式下，可以为全局标识符undefined赋值，undefined是一个内部标识符，它的值为undefined（可以被改变）</p>
<p>通过void运算符可以获得该值</p>
<p>void并不改变表达式的结果，只是让表达式不返回值。</p>
<p>一些应用<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">doSomething</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (!APP.ready) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">void</span> setTimeout( doSomething, <span class="number">100</span>); <span class="comment">// 不返回timeId而是返回undefined</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>一般会分开操作，效果都一样。<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">doSomething</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (!APP.ready) &#123;</span><br><span class="line">    setTimeout( doSomething, <span class="number">100</span>); <span class="comment">// 不返回timeId而是返回undefined</span></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>未完待续…</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/js/" rel="tag"># js</a>
          
            <a href="/tags/前端/" rel="tag"># 前端</a>
          
            <a href="/tags/读书笔记/" rel="tag"># 读书笔记</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/04/11/2018-04-11/" rel="next" title="js中function本身的length属性和arguments.length区别？">
                <i class="fa fa-chevron-left"></i> js中function本身的length属性和arguments.length区别？
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/05/02/2018-5-2-React-redux/" rel="prev" title="《深入浅出React和Redux》读书笔记">
                《深入浅出React和Redux》读书笔记 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="何米酥">
            
              <p class="site-author-name" itemprop="name">何米酥</p>
              <p class="site-description motion-element" itemprop="description">Just do...</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">202</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">78</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/hemisu" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/hemisu" target="_blank" title="知乎">
                      
                        <i class="fa fa-fw fa-globe"></i>知乎</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#typeof-undeclared"><span class="nav-number">1.</span> <span class="nav-text">typeof undeclared</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#小结"><span class="nav-number">2.</span> <span class="nav-text">小结</span></a></li></ol><li class="nav-item nav-level-1"><a class="nav-link" href="#值"><span class="nav-number"></span> <span class="nav-text">值</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#数组"><span class="nav-number">1.</span> <span class="nav-text">数组</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#类数组"><span class="nav-number"></span> <span class="nav-text">类数组</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#字符串"><span class="nav-number">1.</span> <span class="nav-text">字符串</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数字"><span class="nav-number">2.</span> <span class="nav-text">数字</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#0-1-0-2-0-3问题"><span class="nav-number">2.1.</span> <span class="nav-text">0.1+0.2 != 0.3问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#整数的安全范围"><span class="nav-number">2.2.</span> <span class="nav-text">整数的安全范围</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#整数检测"><span class="nav-number">2.3.</span> <span class="nav-text">整数检测</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#32位有符号整数"><span class="nav-number">2.4.</span> <span class="nav-text">32位有符号整数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#特殊数值"><span class="nav-number">3.</span> <span class="nav-text">特殊数值</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#undefined"><span class="nav-number">3.1.</span> <span class="nav-text">undefined</span></a></li></ol></li></ol></li></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">何米酥</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
